package com.yingda.fragment;

import android.content.ContentResolver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.yingda.ad.Constants;
import com.yingda.ad.R;
import com.yingda.bean.WorkBean;
import com.yingda.eventbus.EventBus;
import com.yingda.utils.ExcelUtils;
import com.yingda.utils.ExecutorsUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReadTabTwoNewFragment extends Fragment {
    public static final String TAG = "ReadTabFragment";
    //private List<WorkBean>  workBeanList = new ArrayList<>();

    private Map<String,WorkBean> workBeanList = new HashMap<>();
    private StringBuffer sb = new StringBuffer();
    private TextView tv_content;
    private Handler handler = new Handler(Looper.getMainLooper());

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_table, container, false);

        initView(view);
        initData();

        return view;
    }

    private void initData() {
        ExecutorsUtils.getInstance().getSingleThread(() -> {
            Bundle bundle=getArguments();
            String fileUrl=bundle.getString("fileUrl");
            readFileContent(Uri.parse(fileUrl));
        });
    }

    private void readFileContent(Uri fileUri) {
        ContentResolver contentResolver = getActivity().getContentResolver();
        InputStream inputStream;
        try {
            inputStream = contentResolver.openInputStream(fileUri);
            readExcel(inputStream,new File("222.xlsx"));
        } catch (FileNotFoundException e) {

            Toast.makeText(getActivity(), "读取文件失败: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        }

    }


    //读取excel 的数据
    public void readExcel(File excelPath){
        // String excelPath1 = SharedPreferenceUtil.getInstance(this).getString("excelPath");
//        List<List<Object>> lists = ExcelUtils.getInstance().readExcel(excelPath);
//        Log.i(TAG, "readExcel: " + lists);
    }

    public void   readExcel(InputStream inputStream ,File excelPath){
        // String excelPath1 = SharedPreferenceUtil.getInstance(this).getString("excelPath");
        List<List<Object>> lists = ExcelUtils.getInstance().readExcel(inputStream,excelPath);
        Log.i(TAG, "readExcel: " + lists);
        workBeanList.clear();
        for (List<Object> list : lists) {
            if (list!=null && list.size()>0){
                for (int i = 0; i < list.size(); i++) {
                    String id = (String) list.get(1);
                    String name= (String) list.get(2);
                    WorkBean workBean = new WorkBean();
                    workBean.setId(id);
                    workBean.setName(name);
                    if (!TextUtils.isEmpty(id) && isInteger(id)){
                        workBeanList.put(id,workBean);
                    }

                }
            }
        }

        Log.i(TAG, "readExcel: " + workBeanList);
        if (workBeanList.size()>0){
            for (String key : workBeanList.keySet()) {
                WorkBean workBean = workBeanList.get(key);
                String name = workBean.getName();
                String id = workBean.getId();
                sb.append("name=" + name + "...id=" + id + "\n");
                //System.out.println("Key = " + key + ", Value = " + hm.get(key));
            }
            handler.post(() -> {
                EventBus.getDefault().post("关闭dialog", Constants.DIALOGCLOSE);
                tv_content.setText(sb.toString());
            });
        }else {
            EventBus.getDefault().post("关闭dialog", Constants.DIALOGCLOSE);

        }

        //传递数据
        EventBus.getDefault().post(workBeanList,Constants.READTWO);
    }
    public static boolean isInteger(String str) {
        if (str == null || str.isEmpty()) {
            return false; // 空字符串和 null 不是有效的 int
        }
        try {
            Integer.parseInt(str);
            return true;  // 成功解析为 int
        } catch (NumberFormatException e) {
            return false; // 解析失败，说明不是 int 类型
        }
    }


    private void initView(View view) {
        tv_content = view.findViewById(R.id.tv_content);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }
}
